package bb.lanxing.lib.devices.bici.cmd;

import com.umeng.analytics.pro.cc;
import bb.lanxing.lib.devices.bici.BLECommand;
import bb.lanxing.lib.devices.utils.ByteUtils;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.Map;

public class DeviceControlCMD extends BLECommand implements Serializable {
    public static final byte COMMAND_ID = 2;
    public static final byte KEY_ALARM = 4;
    public static final byte KEY_LIGHT = 2;
    public static final byte KEY_LOCK = 1;
    public static final byte KEY_SOUND = 3;
    public static final byte KEY_STOP_WORKOUT = 5;
    private byte alarm;
    private byte[] lightValue;
    private byte lock;
    private byte sound;

    public DeviceControlCMD() {
        this.commandId = (byte) 2;
    }

    public DeviceControlCMD(byte[] bArr) {
        super(bArr);
    }

    @Override
    public void parseBody(byte[] bArr) {
        if (bArr.length < 3) {
            return;
        }
        ByteBuffer wrap = ByteBuffer.wrap(bArr);
        byte b = wrap.get();
        int i = wrap.getShort();
        byte[] bArr2 = new byte[(bArr.length - i) - 3];
        if (b == 1) {
            this.lock = wrap.get();
        } else if (b == 2) {
            byte[] bArr3 = new byte[i];
            this.lightValue = bArr3;
            wrap.get(bArr3);
        } else if (b == 3) {
            this.sound = wrap.get();
        } else if (b == 4) {
            this.alarm = wrap.get();
        }
        wrap.get(bArr2);
        parseBody(bArr2);
    }

    public String toString() {
        return "lock = " + ((int) this.lock) + "\n lightValue = " + ByteUtils.printlnByteArrayToUnsignHexString(this.lightValue) + "\n sound = " + ((int) this.sound) + "\n alarm = " + ((int) this.alarm);
    }

    @Override
    protected byte[] encodeCMDBody(Map<Byte, Object> map) {
        int i;
        ByteBuffer allocate = ByteBuffer.allocate(500);
        allocate.put((byte) 2);
        allocate.put((byte) 16);//cc.n
        if (map.get((byte) 1) != null) {
            allocate.put((byte) 1);
            allocate.putShort((short) 1);
            allocate.put((Byte) map.get((byte) 1));
            i = 6;
        } else {
            i = 2;
        }
        if (map.get((byte) 2) != null) {
            allocate.put((byte) 2);
            byte[] bArr = (byte[]) map.get((byte) 2);
            allocate.putShort((short) bArr.length);
            allocate.put(bArr);
            i = i + 3 + bArr.length;
        }
        if (map.get((byte) 3) != null) {
            allocate.put((byte) 3);
            allocate.putShort((short) 1);
            allocate.put((Byte) map.get((byte) 3));
            i += 4;
        }
        if (map.get((byte) 4) != null) {
            allocate.put((byte) 4);
            allocate.putShort((short) 1);
            allocate.put((Byte) map.get((byte) 4));
            i += 4;
        }
        if (map.get((byte) 5) != null) {
            allocate.put((byte) 5);
            allocate.putShort((short) 1);
            allocate.put((Byte) map.get((byte) 5));
            i += 4;
        }
        byte[] bArr2 = new byte[i];
        allocate.flip();
        allocate.get(bArr2);
        return bArr2;
    }

    public byte getLock() {
        return this.lock;
    }

    public byte[] getLightValue() {
        return this.lightValue;
    }

    public byte getSound() {
        return this.sound;
    }

    public byte getAlarm() {
        return this.alarm;
    }
}
